#ifndef CONVERTCONTROL_H
#define CONVERTCONTROL_H

#include <BLineArray>
#include <BOctTree>
#include <BQuadTree>
#include <BPolygon>
#include <BSphereHandler>
#include <BGeometry>

using namespace BWE;

namespace BWE
{
	typedef BArray<BTriangle>	BTriangleArray;
	typedef BArray<BQuad>		BQuadArray;
	typedef BArray<BPolygon>	BPolygonArray;

	struct PolyCell : public BThing
	{
		PolyCell()
		{
			element = 0;
		}

		BTriangleArray		triangles;
		BQuadArray			quads;
		BPolygonArray		polygons;
		BElement*			element;
	};

	typedef BOctTree<PolyCell>				PolysOctTree;
	typedef BOctTree<PolyCell>::Item		PolysOctItem;
	typedef BOctTree<PolyCell>::ItemArray	PolysOctItemArray;

	typedef BQuadTree<BIndexArray>				QuadTree;
	typedef BQuadTree<BIndexArray>::Item		QuadTreeItem;
	typedef BQuadTree<BIndexArray>::ItemArray	QuadTreeItemArray;

}

class ConvertHandler : public BSphereHandler
{
public:
	ConvertHandler();
	~ConvertHandler();

	BLineArray& lostLines();

	void setNode(BNode* node);
	BNode* node() const;

	void setArea(const BSpace& area);
	const BSpace& area() const;

	void showArea();
	void hideArea();

protected:
	void drawPolyItem(const PolysOctItem& item, BRender& render);
	void drawQuadItem(const QuadTreeItem& item, BRender& render);

protected:
	virtual void paint(BPainter& painter);
	virtual void render(BRender& render);

	virtual bool mouseEnterEvent(const BMouseEvent& mouseEvent);
	virtual bool mouseLeaveEvent(const BMouseEvent& mouseEvent);
	virtual bool mousePressEvent(const BMouseEvent& mouseEvent);
	virtual bool mouseReleaseEvent(const BMouseEvent& mouseEvent);
	virtual bool mouseMoveEvent(const BMouseEvent& mouseEvent);

private:
	BNodeHolder		_node;
	BLineArray		_lostLines;
	BSpace			_area;
	bool			_areaShown;
	BVector			_hoverPoint;
	bool			_hovering;
	bool			_pressed;
	BVector			_pressedPoint;
	BSpace			_pressedArea;
	BArray<PolysOctItem>	_hitPolyItems;
	BHolder<BTerrain>		_hitTerrain;
	BArray<QuadTreeItem>	_hitQuadItems;
};

#endif